package com.ssyx.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ssyx.model.product.Category;
import com.ssyx.model.product.SkuInfo;
import com.ssyx.product.service.CategoryService;
import com.ssyx.product.service.SkuInfoService;
import com.ssyx.vo.product.SkuInfoVo;
import com.ssyx.vo.product.SkuStockLockVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName:ProductInnnerController
 * Description:
 * 远程调用
 * @Author kay
 * @Create 2024/5/3 20:42
 * @Version 1.0
 */
@RestController
@RequestMapping("/api/product")
public class ProductInnerController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SkuInfoService skuInfoService;

    @GetMapping("inner/getCategory/{categoryId}")
    public Category getCategory(@PathVariable Long categoryId) {
        return categoryService.getById(categoryId);
    }

    //根据商品可将可将skuId得到商品skuInfo信息
    @GetMapping("inner/getSkuInfo/{skuId}")
    public SkuInfo getSkuInfo(@PathVariable("skuId") Long skuId) {
        return skuInfoService.getById(skuId);
    }

    //根据skuIdList,批量获取商品信息
    @PostMapping("inner/findSkuInfoList")
    public List<SkuInfo> findSkuInfoList(@RequestBody List<Long> skuIdList){
        List<SkuInfo> skuInfoList = skuInfoService.listByIds(skuIdList);
        return skuInfoList;
    }

    //根据关键字获取sku列表，活动使用
    @GetMapping("inner/findSkuInfoByKeyword/{keyword}")
    public List<SkuInfo> findSkuInfoByKeyword(@PathVariable("keyword") String keyword) {
        LambdaQueryWrapper<SkuInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(SkuInfo::getSkuName,keyword);
        List<SkuInfo> skuInfoList = skuInfoService.list(wrapper);
        return skuInfoList;
    }

    //根据分类id的集合,获取商品分类集合信息
    @GetMapping("inner/getCategoryList")
    public List<Category> getCategoryList(@RequestBody List<Long> categoryIdList){
        List<Category> categoryList = categoryService.listByIds(categoryIdList);
        return categoryList;
    }

    //获取分类信息
    @GetMapping("inner/findAllCategoryList")
    public List<Category> findAllCategoryList() {
        return categoryService.findAllList();
    }

    //获取新人专享
    @GetMapping("inner/findNewPersonSkuInfoList")
    public List<SkuInfo> findNewPersonSkuInfoList() {
        return skuInfoService.findNewPersonList();
    }

    //获取销量高的商品
    @GetMapping("inner/findMaxStockList")
    public List<SkuInfo> findMaxStockList(){
        return skuInfoService.findMaxStockList();
    }

    //根据仓库id + 分类id 进行查询
    @GetMapping("inner/findByWareIdAndCategoryId/{categoryId}/{wareId}/{page}/{limit}")
    public Page<SkuInfo> findByWareIdAndCategoryId(@PathVariable Long categoryId,
                                                   @PathVariable Long wareId,
                                                   @PathVariable Long page,
                                                   @PathVariable Long limit){

        Page<SkuInfo> pageParams = new Page<>(page,limit);
        Page<SkuInfo> skuInfoPage = skuInfoService.getSkuInfoPage(pageParams,categoryId,wareId);
        return skuInfoPage;
    }

    //根据仓库id + keyword 查询
    @GetMapping("inner/findByWareIdAndKeyword/{keyword}/{wareId}/{page}/{limit}")
    public Page<SkuInfo> findByWareIdAndKeyword(@PathVariable String keyword,
                                                @PathVariable Long wareId,
                                                @PathVariable Long page,
                                                @PathVariable Long limit){
        Page<SkuInfo> pageParams = new Page<>(page,limit);
        Page<SkuInfo> skuInfoPage = skuInfoService.selectSkuInfoPage(pageParams,keyword,wareId);
        return skuInfoPage;
    }

    //获取商品sku信息
    @GetMapping("inner/getById/{id}")
    public SkuInfoVo getById(@PathVariable Long id){
        SkuInfoVo skuInfoVo = skuInfoService.getSkuInfoVoById(id);
        return skuInfoVo;
    }

    //根据商品skuId,获得其分类id
    @GetMapping("inner/getCategoryIdBySkuId/{skuId}")
    public Long getCategoryIdBySkuId(@PathVariable Long skuId){
        SkuInfo skuInfo = skuInfoService.getById(skuId);
        Long categoryId = skuInfo.getCategoryId();
        return categoryId;
    }

    //根据用户提交的相关购物车商品信息,判断是否锁定库存
    @PostMapping("inner/checkAndLock/{orderNo}")
    public Boolean checkAndLock(@RequestBody List<SkuStockLockVo> skuStockLockVoList,
                                @PathVariable String orderNo) {
        return skuInfoService.checkAndLock(skuStockLockVoList, orderNo);
    }

    //根据orderNo,修改产品表库存
    @PostMapping("inner/updateStockByOrderNo/{orderNo}")
    public Boolean updateStockByOrderNo(@PathVariable String orderNo){
        skuInfoService.updateStockByOrderNo(orderNo);
        return true;
    }
}
